<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:c="livcos.org/ns/cosmos"
	xmlns:job="livcos.org/ns/process/job"
	xmlns:f="livcos.org/ns/xslt/function"
	exclude-result-prefixes="job c f">
  
  <xsl:import href="/livcos.org/common/base.xsl"/>
  
  <xsl:template match="/">
    <html>
      <head>
      	<title>Crawl <xsl:value-of select="resource/@uri"/></title>
      	<meta name="to-root-path" content="{$toRootPath}"/>
      	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      	<link href="{f:caller-uri('/livcos.org/web/css/styles.css')}" rel="stylesheet" type="text/css"/>
      	<link href="{f:caller-uri(resolve-uri('css/styles.css'))}" rel="stylesheet" type="text/css"/>
      	<script>
      		window.onload = function() {
      			var report = document.getElementById("report");
	      		var links = document.body.getElementsByTagName("a");
	      		for (var i = 1; i &lt; links.length; i++) {
		      		var req = new XMLHttpRequest();
		      		req._link = links[i];
		      		req._report = report;
		      		req.onreadystatechange = function() {
			      		if (this.readyState == 4) {
			      			this._link.innerHTML += " (" + this.status + ": " + this.statusText + ")";
			      			if (this.statusText != "OK") {
			      				this._report.innerHTML += this._link.title + ": " + this.statusText + "&lt;br/>";
			      			}
			      		}
		      		};
		      		req.open("GET", links[i].title, true);
		      		req.send(null);
	      		}
      		}
      		
      		function expandAll() {
	      		var divs = document.body.getElementsByTagName("div");
	      		for (var i = 0; i &lt; divs.length; i++) {
		      		divs[i].style.display = "block";
	      		}
      		}
      	</script>
      </head>
      <body>
      	<h1>Crawl <xsl:value-of select="resource/@uri"/></h1>
      	<hr/>
      	<xsl:variable name="problems" select=".//resource[@missing or exception]"/>
      	<xsl:if test="$problems">
      		<div class="list">
      			<xsl:apply-templates select="$problems"/>
      		</div>
      	</xsl:if>
      	<div id="report" style="font-size: 80%"></div>
      	<hr/>
      	<div class="list">
      		<div style="text-align: right"><a href="javascript:expandAll()">expand all</a></div>
     			<xsl:apply-templates select="resource"/>
      	</div>
      </body>
    </html>
  </xsl:template>
	
	<xsl:template match="resource">
		<div class="res">
			<div class="state">
				<xsl:choose>
					<xsl:when test="exception">
						<span style="color: red">
							<xsl:value-of select="exception/@type"/>
						</span>
						<xsl:if test="normalize-space(exception/message) != ''">
							<xsl:text>: </xsl:text>
							<xsl:value-of select="exception/message"/>
						</xsl:if>
					</xsl:when>
					<xsl:otherwise><xsl:apply-templates select="@* except @uri"/></xsl:otherwise>
				</xsl:choose>
			</div>
			<div class="uri">
				<xsl:choose>
					<xsl:when test="resource">
						<span onclick="var s = this.parentNode.nextSibling.style; s.display = (s.display == 'none')?'block':'none';">
							<xsl:value-of select="@uri"/>
						</span>
						<xsl:text> (</xsl:text>
						<xsl:variable name="level" select="count(ancestor::resource)"/>
						<xsl:variable name="depths" select="for $r in .//resource[not(resource)] return count($r/ancestor::resource)"/>
						<xsl:value-of select="max($depths) - $level"/>
						<xsl:text>)</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="@uri"/>
					</xsl:otherwise>
				</xsl:choose>
			</div>
			<xsl:if test="resource">
				<div style="display: {if (.//exception) then 'block' else 'none'}">
					<xsl:apply-templates select="resource"/>
				</div>
			</xsl:if>
		</div>
	</xsl:template>
	
	<xsl:template match="@*">
		<xsl:if test="position() > 1">, </xsl:if>
		<xsl:value-of select="name(.)"/>
	</xsl:template>
	
	<xsl:template match="@filter">
		<xsl:if test="position() > 1">, </xsl:if>
		<xsl:text>filtered by filter </xsl:text>
		<xsl:value-of select="."/>
	</xsl:template>
	
	<xsl:template match="@missing">
		<xsl:if test="position() > 1">, </xsl:if>
		<span style="color: red"><xsl:value-of select="name(.)"/></span>
	</xsl:template>
	
	<xsl:template match="@external">
		<xsl:if test="position() > 1">, </xsl:if>
		<a title="{../@uri}" onclick="open('{../@uri}', 'preview', 'width=400,height=200,scrollbars=yes')">
			<xsl:value-of select="name(.)"/>
		</a>
	</xsl:template>
	
</xsl:stylesheet>